import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/product_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:get/get.dart';

class WarehouseDetailsController extends GetxController {
  /// 产品仓库id
  String id = Get.arguments as String;

  /// 仓库实体
  WarehouseEntity warehouseEntity = WarehouseEntity();

  /// 产品储存量
  List<ProductEntity> productList = [];

  /// 进入出库记录页面
  void toOutboundRecordsPage() {
    Get.toNamed(AppRoutes.outboundRecords, arguments: {'warehouseId': id});
  }

  @override
  void onInit() {
    super.onInit();
    // 获取仓库信息
    _getWarehouseRecord();
    // 获取产品储存量
    _getProductStorage();
    update();
  }

  /// 获取仓库信息
  void _getWarehouseRecord() async {
    final res = await HttpUtil().generateGetRequest<WarehouseEntity>(
      queryParameters: {'id': id},
      Interface.getWarehouseRecord,
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        warehouseEntity = result.data!;
      }
    });
    update();
  }

  /// 获取产品储存量
  _getProductStorage() async {
    final res = await HttpUtil().generatePostRequest<List<ProductEntity>>(
      Interface.getProductByPage,
      data: {
        'wareHouseId': id,
        'pageNum': 1,
        'pageSize': 1000,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        productList = result.data!;
        update();
      }
    });
  }
}
